<?php

class Admin_Add_City extends Controller
{
	var $data=array();
	
	public function __construct()
	{
		parent::Controller();
        include("password_protect.php");
		$this->load->model('Admin_Input_Model');
		$this->load->model('Admin_Quotation_Model');
		
		$this->data['warning']="";
		$this->data['success']="";
		$this->data['city_exists']="";
		$this->data['city']="";
		$this->data['province_list']=$this->get_province_list();
		
		$this->data['province_exists']="";
		$this->data['province_state']="";
		
	}	

	function index()
	{
		if($this->input->post('add_city'))
		{
			$this->data['city']=trim($this->input->post('city'));
			$province_dropdown = ($this->data['province_list'][$this->input->post('province_list')]);

			//search if this exists
			if(strcmp($province_dropdown, "")!=0 && strcmp($this->data['city'],"")!=0)
			{
				$result=$this->Admin_Input_Model->search_city($this->data['city'], $province_dropdown);
				
				if($result>0)
				{
					$this->data['warning']="City ".$this->data['city']." under ".$province_dropdown." province/State already exists.";
				}
				else
				{
					//add this city under the province
					if($this->Admin_Input_Model->add_city($this->data['city'], $this->data['province_list'][$this->input->post('province_list')]))
						$this->data['success']="City ".$this->data['city']." was added successfully to the Province/State ".$this->data['province_list'][$this->input->post('province_list')];
					else
						$this->data['warning']=$this->data['warning']."\nSomething went wrong while adding the city";
				}
			}
			else if(strcmp($this->data['province_list'][$this->input->post('province_list')], "")==0)
			{
				$this->data['warning']="Province not selected.";
			}
			else if(strcmp($this->data['city'],"")==0)
			{
				$this->data['warning']="City not typed in.";
			}
		}
		
		if($this->input->post('add_province'))
		{
			$this->data['province_state']=trim($this->input->post('province_state'));
			$this->data['country']=trim($this->input->post('country'));
			
			if(strcmp($this->data['province_state'], "")!=0 && strcmp($this->data['country'],"")!=0)
			{
				//checking if a duplicate already exists
				$result=$this->Admin_Input_Model->search_province_state($this->data['province_state'], $this->data['country']);

				if($result>0)
				{
					$this->data['warning']="Province/State \"".$this->data['province_state']."\" under \"".$this->data['country']."\" already exists.";
				}
				else
				{
					if($this->Admin_Input_Model->add_province($this->data['province_state'], $this->data['country']))
					{
						$this->data['province_list']=$this->get_province_list();
						$this->data['success']="Province ".$this->data['province_state']." was added successfully to the Country ".$this->data['country'];
					}
					else
						$this->data['warning']=$data['warning']."\nsomething went wrong while adding the province/state";
				}
			}
			else if(strcmp($this->data['province_state'],"")==0)
			{
				$this->data['warning']="Province/State name was not entered.";
			}
			else if(strcmp($this->data['country'],"")==0)
			{
				$this->data['warning']="Country name was not entered.";
			}
		}

	
		$this->load->view("admin_add_city",$this->data);
	}
	
	function get_province_list()
	{
		return $this->Admin_Input_Model->get_province_list();
	}

}